package com.hb.hb_banner;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;
import com.wonders.ips.common.DbConnection;

/**
 * Data access object (DAO) for domain model class HbBanner.
 * @see com.hb.hb_banner.HbBanner
 * @author MyEclipse - Hibernate Tools
 */
public class HbBannerDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(HbBannerDAO.class);

	//property constants
	public static final String TITLE = "title";
	public static final String FILENAME = "filename";
	public static final String URL = "url";
	public static final String TYPE = "type";
	public static final String PRICE = "price";
	
	Transaction tx=null;

    
    public void save(HbBanner transientInstance) {
        log.debug("saving HbBanner instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(HbBanner persistentInstance) {
        log.debug("deleting HbBanner instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public HbBanner findById( java.lang.Integer id) {
        log.debug("getting HbBanner instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbBanner instance = (HbBanner) getSession()
                    .get("com.hb.hb_banner.HbBanner", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(HbBanner instance) {
        log.debug("finding HbBanner instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_banner.HbBanner")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding HbBanner instance with property: " + propertyName
            + ", value: " + value);
      try {
    	  tx=getSession().getTransaction();
      	tx.begin();
         String queryString = "from HbBanner as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}
	
	public List findByFilename(Object filename) {
		return findByProperty(FILENAME, filename);
	}
	
	public List findByUrl(Object url) {
		return findByProperty(URL, url);
	}
	
	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}
	
	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}
	
    public HbBanner merge(HbBanner detachedInstance) {
        log.debug("merging HbBanner instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbBanner result = (HbBanner) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(HbBanner instance) {
        log.debug("attaching dirty HbBanner instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(HbBanner instance) {
        log.debug("attaching clean HbBanner instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}